//Edit to limit this function to return an array with results of frames from translation.
translate = function(dnaRna)
	{
	console.log('translating '+dnaRna );
	//get rid of white spaces
	dnaRna = dnaRna.replace(/[\s\n\r]/g,"");
	return runTranslate(dnaRna); 
	}

function runTranslate(sequenceIn)
	{
	//change to upper case and RNA notation
	var sequence = sequenceIn.toUpperCase();
	sequence = sequence.replace(/T/g, "U");
	
	//Save in array for returning
	var arrSol =[];
	
	//Frames 1-3
	arrSol.push(myTranslate( sequence, 0 ));
	arrSol.push(myTranslate( sequence, 1 ));
	arrSol.push(myTranslate( sequence, 2 ));
	
	//create complimentary translations Frames 4-6
	sequence = changeOrigin(sequence);
	arrSol.push(myTranslate( sequence, 0 ));
	arrSol.push(myTranslate( sequence, 1 ));
	arrSol.push(myTranslate( sequence, 2 ));
				
	return arrSol;
	}

function myTranslate( seq, start )
	{
	
	var cTable = codonTable();
	
	var solution = [];
	
	for( var i=start; i+3<seq.length; i+=3 )
		{
		var codon =  seq.slice(i,i+3);
		if( cTable[codon] == undefined )
			{
			solution.push( 'X' );
			}
		else
			{
			solution.push(cTable[codon]);
			}
		}
	
	solution = solution.join("");
	
	return solution;
	}
	
function changeOrigin( seq )
	{
	seq = seq.split("").reverse();
	return seq.map(changeLetter).join("");
	}
function changeLetter( let )
	{
	if( let == "A" )
		return "U";
	if( let == "U" )
		return "A";
	if( let == "G" )
		return "C";
	if( let == "C" )
		return "G";
	return let;
	}

function codonTable()
	{
	var codons = 
		{"A" : ["GCU", "GCC", "GCA", "GCG"],
		"L" : ["UUA", "UUG", "CUU", "CUC", "CUA", "CUG"],
		"R" : ["CGU", "CGC", "CGA", "CGG", "AGA", "AGG"],
		"K" : ["AAA", "AAG"],
		"N" : ["AAU", "AAC"],
		"M" : ["AUG"],
		"D" : ["GAU", "GAC"],
		"F" : ["UUU", "UUC"],
		"C" : ["UGU", "UGC"], 	
		"P" : ["CCU", "CCC", "CCA", "CCG"],
		"Q" : ["CAA", "CAG"],
		"S" : ["UCU", "UCC", "UCA", "UCG", "AGU", "AGC"],
		"E" : ["GAA", "GAG"],
		"T" : ["ACU", "ACC", "ACA", "ACG"],
		"G" : ["GGU", "GGC", "GGA", "GGG"],
		"W" : ["UGG"],
		"H" : ["CAU", "CAC"], 	
		"Y" : ["UAU", "UAC"],
		"I" : ["AUU", "AUC", "AUA"], 
		"V" : ["GUU", "GUC", "GUA", "GUG"],
		"*" : ["UGA","UAG","UAA"]
		};
		
	var aminos = {};
	for( var AA in codons )
		{
		for( var trip in codons[AA] )
			{
			aminos[codons[AA][trip]] = AA;
			}
		}
	return aminos;
	}